import cv2
import os
import time
from sio import sio 
from loguru import logger
from app.core.conf import ST
from app.utils.generate_string import get_cur_timestamp
from rasip.camera import get_rasip_photo
from cv.image.BaseImg import BaseImg





async def handle_dev_window_long_camera(station,dev,timestamp):
    img_raw_1 = get_rasip_photo(ST.LONG_CAMERA_URL)
    baseimg_1 = BaseImg(dev=dev,station=station,img_raw=img_raw_1,timestamp=timestamp)
    filter_img_1 = baseimg_1.detect_glass_new()

    img_raw_2 = get_rasip_photo(ST.LONG_CAMERA_URL)
    baseimg_2 = BaseImg(dev=dev,station=station,img_raw=img_raw_2,timestamp=timestamp)
    filter_img_2 = baseimg_2.detect_glass_new()

    img_final = cv2.addWeighted(filter_img_1,0.5,filter_img_2,0.5,0)
    cv2.imwrite(ST.IMG_DRAW_PATH+"\\"+str(timestamp)+'.jpg',img_final)
    await sio.emit("cv_add", f'http://10.12.48.223:7000/img/{timestamp}.jpg', room="agv")

    return False


    # img_byte = get_rasip_photo(ST.LONG_CAMERA_URL,True)
    img_name_1 = f'image_{get_cur_timestamp()}.jpg'
    img_path_1 = os.path.join(ST.IMG_DRAW_PATH, img_name_1)
    save_image(img_byte,img_path_1)

    time.sleep(1)


    img_byte =  get_rasip_photo(ST.SHORT_CAMERA_URL,True)
    # img_byte = get_rasip_photo(ST.LONG_CAMERA_URL,True)
    img_name_2 = f'image_{get_cur_timestamp()}.jpg'
    img_path_2 = os.path.join(ST.IMG_DRAW_PATH, img_name_2)
    save_image(img_byte,img_path_2)

    time.sleep(0.5)

    img_byte = get_rasip_photo(ST.SHORT_CAMERA_URL,True)
    # img_byte = get_rasip_photo(ST.LONG_CAMERA_URL,True)
    img_name_3 = f'image_{get_cur_timestamp()}.jpg'
    img_path_3 = os.path.join(ST.IMG_DRAW_PATH, img_name_3)
    save_image(img_byte,img_path_3)

    time.sleep(0.5)


    img_byte = get_rasip_photo(ST.SHORT_CAMERA_URL,True)
    # img_byte = get_rasip_photo(ST.LONG_CAMERA_URL,True)
    img_name_4 = f'image_{get_cur_timestamp()}.jpg'
    img_path_4 = os.path.join(ST.IMG_DRAW_PATH, img_name_4)
    save_image(img_byte,img_path_4)


    # 读取两张图片
    img1 = cv2.imread(img_path_1)
    img2 = cv2.imread(img_path_2)
    img3 = cv2.imread(img_path_3)
    img4 = cv2.imread(img_path_4)

    
    # 计算多张图片的差异
    diff1 = cv2.subtract(img1, img2)
    diff2 = cv2.subtract(img1, img3)
    diff3 = cv2.subtract(img1, img4)

    await save_diff(diff1,diff2,diff3)





async def save_diff(diff1,diff2,diff3):
    # 保存合并后的图片
    img_name1 = f'diff_{get_cur_timestamp()}.jpg'
    img_path1= os.path.join(ST.IMG_DRAW_PATH, img_name1)
    # 保存差异图像
    cv2.imwrite(img_path1, diff1)

    img_name2 = f'diff_{get_cur_timestamp()}.jpg'
    img_path2= os.path.join(ST.IMG_DRAW_PATH, img_name2)
    # 保存差异图像
    cv2.imwrite(img_path2, diff2)


    img_name3 = f'diff_{get_cur_timestamp()}.jpg'
    img_path3= os.path.join(ST.IMG_DRAW_PATH, img_name3)
    # 保存差异图像
    cv2.imwrite(img_path3, diff3)

    # 机台里面的识别，待做
    # await sio.emit('change_inside',{
    #     "img_urls":[f'http://10.12.48.223:9999/img/raw/{img_name1}',f'http://10.12.48.223:9999/img/raw/{img_name2}',f'http://10.12.48.223:9999/img/raw/{img_name3}']
    # })

    await sio.emit("cv_add", f'http://10.12.48.223:7000/img/{img_name1}', room="agv")



def save_image(content, filename):
    with open(filename, 'wb') as f:
        f.write(content)





    
    


